package com.qk.management.controller;

import com.qk.result.PageResult;
import com.qk.result.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @Author:zhengbowen
 * @Date: 2025/10/10 19:36
 * @Description: 部门管理控制器
 */
@RestController
@RequestMapping("/depts")
@RequiredArgsConstructor
@Slf4j
public class DeptController {

    private final DeptService deptService;

    @PostMapping()
    public Result insertDept(@RequestBody Dept dept) {
        deptService.insertDept(dept);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectDeptsList() {
        List<Dept> depts = deptService.selectDeptsList();
        return Result.success(depts);
    }

    @GetMapping("")
    public Result selectDeptsByPage(@RequestParam(defaultValue = "1", value = "page") Integer page,
                                    @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                                    @RequestParam(value = "name", required = false) String name,
                                    @RequestParam(value = "status", required = false) Integer status
    ) {
        PageResult<Dept> pageResult = deptService.selectByPage(page, pageSize, name, status);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        deptService.deleteDept(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result SelectDeptById(@PathVariable String id) {
        Dept dept = deptService.selectDeptById(id);
        return Result.success(dept);
    }

    @PutMapping("")
    public Result updateDept(@RequestBody Dept dept) {
        deptService.updateDept(dept);
        return Result.success();
    }

}